package Test9;

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Test9 {

        public static void main(String[] args) throws InterruptedException {
            int num=5;
            CountDownLatch start=new CountDownLatch(1);
            CountDownLatch end=new CountDownLatch(5);


            for(int i=1;i<=5;i++){
                Random random=new Random();
                int a=random.nextInt(1000);
                int id=i;
                new Thread(()->{
                    try {
                        System.out.println(id+"开始准备");
                        start.await();

                        System.out.println(id+"开始跑步");
                        Thread.sleep(1000+a);

                        System.out.println(id+"到达终点");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }).start();
            }

            System.out.println("裁判员发出比赛开始信号");
            start.countDown();


            end.countDown();
            System.out.println("所有选手跑完比赛，比赛结束");
        }










    public static void main03(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                for(int i=0;i<10000;i++){
                    a++;
                }
                return a;
            }
        };

        FutureTask<Integer> task=new FutureTask<>(callable);
        Thread t=new Thread(task);

        t.start();
        t.join();

        System.out.println(a);
        System.out.println(task.get());
    }









    static int a=0;
    public static void main02(String[] args) throws InterruptedException {
        AtomicInteger atomic=new AtomicInteger(a);



       Thread t=new Thread(()->{
            for(int i=0;i<10000;i++) {
                atomic.getAndAdd(1);
            }
        });
        t.start();
        Thread t2=new Thread(()->{
            for(int i=0;i<10000;i++) {
                atomic.getAndAdd(1);
            }
        });
        t2.start();

        t.join();
        t2.join();


        System.out.println(a);
        System.out.println(atomic.get());


    }












    static int count=1;
    public static void main01(String[] args) throws InterruptedException, ExecutionException {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {

                int kind=1;
                while(kind++<=1000){
                    count=2*count+1;
                }
                return count;
            }
        };
        FutureTask<Integer> futureTask=new FutureTask<>(callable);

        int retult=futureTask.get();

        System.out.println(retult);
    }
}
